package sunwin.yoms.web.controller.resource;

import net.coobird.thumbnailator.Thumbnails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import sunwin.yoms.comm.FileHelper;
import sunwin.yoms.comm.YomsException;
import sunwin.yoms.comm.YomsFunction;
import sunwin.yoms.comm.YomsPage;
import sunwin.yoms.domain.resource.StadiumDomain;
import sunwin.yoms.service.comm.YomsDefaultData;
import sunwin.yoms.service.resource.StadiumService;
import sunwin.yoms.web.comm.R;
import sunwin.yoms.web.comm.YomsAjaxAnswer;
import sunwin.yoms.web.comm.YomsBase64Image;
import sunwin.yoms.web.comm.YomsController;
import sunwin.yoms.web.comm.resolverArg.YomsArrayParam;
import sunwin.yoms.web.comm.resolverArg.YomsTimeParam;

import java.io.File;
import java.io.IOException;
import java.sql.Time;
import java.util.List;

/**
 * Created by watson on 2015/8/14.
 */
@Controller
public class StadiumController {
    @Autowired
    private StadiumService stadiumService;
    private final String FindSvm = "resource/stadium.find";
    private final String CreateSvm = "resource/stadium.create";
    private final String EditSvm = "resource/stadium.edit";

    //查找场馆
    @RequestMapping(value = R.FindStadium, method = RequestMethod.GET)
    @YomsController(functionNo = YomsFunction.FindStadium, viewName = FindSvm)
    public ModelAndView findStadium(StadiumDomain stadium,
                                    @RequestParam(required = false, defaultValue = "1") int pageIndex,
                                    @YomsTimeParam Time startTime,
                                    @YomsTimeParam Time endTime,
                                    ModelMap modelMap) throws YomsException {
        modelMap.addAttribute("stadiums", stadium);
        YomsPage yomsPage = new YomsPage();
        yomsPage.setPageIndex(pageIndex);
        Time time1 = startTime == null ? null : startTime;
        Time time2 = endTime == null ? null : endTime;
        List<StadiumDomain> stadiumDomains = stadiumService.findStadium(stadium, yomsPage, time1, time2);
        ModelAndView mav = new ModelAndView();
        mav.addObject("yomsPage", yomsPage);
        mav.addObject("stadiumList", stadiumDomains);
        return mav;
    }

    //创建场馆访问页面
    @RequestMapping(value = R.CreateStadium, method = RequestMethod.GET)
    @YomsController(viewName = CreateSvm)
    public void createStadium() {
    }

    //创建场馆
    @RequestMapping(value = R.CreateStadium, method = RequestMethod.POST)
    @YomsController(functionNo = YomsFunction.CreateStadium, viewName = CreateSvm, isAjax = true)
    @ResponseBody
    public YomsAjaxAnswer createStadium(StadiumDomain stadiumDomain,
                                        @YomsTimeParam Time time1,
                                        @YomsTimeParam Time time2) throws YomsException {
        String img = stadiumDomain.getStadiumImg();
        String content = stadiumDomain.getStadiumContent();
        stadiumDomain.setStadiumImg("");
        stadiumDomain.setStadiumContent("");
        stadiumDomain.setStadiumStarttime(time1);
        stadiumDomain.setStadiumEndtime(time2);


        stadiumService.createSatdium(stadiumDomain);

        stadiumDomain.setStadiumImg(img);
        stadiumDomain.setStadiumContent(content);
        eidtStadium(stadiumDomain, time1, time2);

        YomsAjaxAnswer yomsAjaxAnswer = new YomsAjaxAnswer();
        yomsAjaxAnswer.setSuccess(true);
        return yomsAjaxAnswer;
    }

    //编辑访问场馆页面
    @RequestMapping(value = R.EditStadium, method = RequestMethod.GET)
    @YomsController(functionNo = YomsFunction.EditStadium, viewName = EditSvm)
    public ModelAndView editStadium(@RequestParam(required = false, defaultValue = "-1") int id) throws YomsException {
        StadiumDomain stadiumDomain = stadiumService.getStadiumById(id);
        ModelAndView mav = new ModelAndView();
        mav.addObject("stadium", stadiumDomain);
        return mav;
    }

    //编辑场馆
    @RequestMapping(value = R.EditStadium, method = RequestMethod.POST)
    @YomsController(functionNo = YomsFunction.EditStadium, viewName = EditSvm, isAjax = true)
    @ResponseBody
    public YomsAjaxAnswer eidtStadium(StadiumDomain stadiumDomain,
                                      @YomsTimeParam Time time1,
                                      @YomsTimeParam Time time2) throws YomsException {
        recompileImg(stadiumDomain);
        stadiumDomain.setStadiumStarttime(time1);
        stadiumDomain.setStadiumEndtime(time2);
        stadiumService.upSatdium(stadiumDomain);
        YomsAjaxAnswer yomsAjaxAnswer = new YomsAjaxAnswer();
        yomsAjaxAnswer.setSuccess(true);
        return yomsAjaxAnswer;
    }

    //删除场馆
    @RequestMapping(value = R.DelStadium, method = RequestMethod.POST)
    @YomsController(functionNo = YomsFunction.DelStadium)
    @ResponseBody
    public YomsAjaxAnswer delStadium(@YomsArrayParam List<Integer> ids) throws YomsException {
        if (ids != null) {
            for (int id : ids) {
                stadiumService.deleteSatdium(id);
                delSatdiumImages(id, "content");
                delSatdiumImages(id, "thumbnail");
            }
        }
        YomsAjaxAnswer yomsAjaxAnswer = new YomsAjaxAnswer();
        yomsAjaxAnswer.setSuccess(true);
        return yomsAjaxAnswer;
    }

    /**
     * 删除场馆相关图片
     *
     * @param
     */
    public void delSatdiumImages(int id, String folder) {
        //获取图片路径
        File file = new File(YomsDefaultData.WebRoot, String.format("img/stadium/%s/%s", folder, id));
        if (file.exists()) {
            String imgPath = file.getAbsolutePath();
            FileHelper.delFolder(imgPath);
        }
    }

    //重新编译
    private void recompileImg(StadiumDomain stadiumDomain) {
        if (stadiumDomain.getStadiumContent() != null) {
            stadiumDomain.setStadiumContent(YomsBase64Image.recompileImgSrc(
                    stadiumDomain.getStadiumContent(),
                    stadiumDomain.getStadiumId(),
                    "stadium/content").getValue());
        } else {
            stadiumDomain.setStadiumContent("");
        }
        String img = YomsBase64Image.restoreBase64code(stadiumDomain.getStadiumImg());
        if (stadiumDomain.getStadiumImg() != null &&
                !stadiumDomain.getStadiumImg().contains("default/default-news.png")) {
            String value = YomsBase64Image.recompileImgSrc(img, stadiumDomain.getStadiumId(), "stadium/thumbnail").getValue();

            String imgPage = YomsDefaultData.WebRoot + YomsBase64Image.getSrcValue(value);
            try {
                Thumbnails.of(imgPage).size(570, 320).toFile(imgPage);
            } catch (IOException e) {
                e.printStackTrace();
            }

            stadiumDomain.setStadiumImg(YomsBase64Image.getSrcValue(value));
        } else {
            stadiumDomain.setStadiumImg("");
        }
    }
}
